<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/10/30
 * Time: 11:08
 */
namespace app\home\controller;

use app\admin\model\Members;
use app\api\controller\HX;
use think\Cache;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Game extends Base
{
    /**
     * pc蛋蛋
     */
    public function eight(){
        
        $game_status = Db::name('cate')->where('id',1)->value('status');
        $cate_id = 1;
        $data = Db::name('hall')
            ->where(array('status'=>1,'cate'=>$cate_id))
            ->select();
        //计算在线人数总人数
        $online_all = Db::name('hall')
            ->sum('online');
        //计算在线人数本种游戏类型的人数
        $online_cate =Db::name('hall')
            ->where(array('status'=>1,'cate'=>$cate_id))
            ->sum('online');
        $return_data['online_all']  =  $online_all;
        $return_data['online_cate'] = $online_cate;
        $return_data['data']        = $data;
        $ads = $this->get_ads();
        return view('',[
            'return_data'=>$return_data,
            'cate'=>$cate_id,
            'ads'=>$ads,
            'game_status'=>$game_status
        ]);
    }

    /**
     * pc蛋蛋房间
     */
    public function eight_hall(Request $request){
        $cate = 1;
        $uid     = session('uid');
        $hall    = $request->param('hall');
        web_check();
        $user_info = Db::name('member')->field('money,head,mobile')->where('id',$uid)->find();
        $balance  = $user_info['money'];
        if($user_info['head']) {
            $head = Config::get('img_url') . $user_info['head'];
        }else{
            $head="";
        }
        $username = $user_info['mobile'];

        $number = getNumberCache('eight');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr_s = explode(',',$numbers['number']);
        $number_arr = getEightNum($number_arr_s);
        //计算下次期数 和 开间时间
  //      $ssc = setEightStageTime($stage,$numbers['dateline']);
        $total_number = array_sum($number_arr);
        $dx = eightDx($total_number); //判断大小
        $ds = eightDs($total_number);//判断单双
        $bo = eightBo($total_number)."波";//判断波色
        $detail = $total_number.",".$dx.",".$ds.","."--".",".$bo;
//        $stage_data = [
//            'stage'=>$stage,
//            'stage_next'=>$ssc['stage_next'],
//            'number'=>$number_arr,
//            'number_sum'=>array_sum($number_arr),
//            'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-10),
//            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//            'detail'=>$detail,
//            'balance'=>$balance,
//            'head'=>$head
//        ];
        $next = $this->get_next(1);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>$detail,
            'balance'=>$balance,
            'head'=>$head
        ];
       //获取赔率
        $odds = Db::name('bet')
            ->field('title,type')
            ->where(array('cate'=>$cate))
            ->select();
        foreach ($odds as $k=>$v){
            $name= Db::name('odds')
                ->where(array('type'=>$v['type'],'cate'=>$cate,'hall'=>$hall))
                ->select();
            foreach ($name as $kk=>$vv){
                $name[$kk]['rate'] =number_format($vv['rate'],3);
            }
            if($name){
                $odds[$k]['name'] = $name;
            }else{
                unset($odds[$k]);
            }
        }
        $odds = array_values($odds);
        $room = Cache::get('chat_room'.$cate.$hall);
        if(!$room){
            $room = "";
        }else {
            $room = array_slice($room, -3);
        }
        $hall_info = Db::name('hall')
            ->field('name,chat_id')
            ->where(['cate'=>$cate,'hall'=>$hall])
           ->find();
        $hall_name = $hall_info['name'];
        $room_id   = $hall_info['chat_id'];
        $number = Db::name('at_eight')
            ->limit(8)
            ->order('id desc')
            ->select();
        foreach ($number as $k => $v) {
            $number_arr_s = explode(',', $v['number']);
            $total_number = array_sum(getEightNum($number_arr_s));
            $dx = eightDx($total_number); //判断大小
            $ds = eightDs($total_number);//判断单双
            $bo = eightBo($total_number);//判断波色
            $detail = $total_number.",".$dx.",".$ds.","."--".",".$bo;
            $number[$k]['number']   = getEightNum($number_arr_s);
            $number[$k]['number_sum'] = $total_number;
            $number[$k]['detail']   = $detail;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds,
            'hall'=>$hall,
            'room'=>$room,
            'hall_name'=>$hall_name,
            'cate'=>$cate,
            'room_id'=>$room_id,
            'username'=>$username,
            'number'=>$number
        ]);
    }

    /**
     * 加拿大28
     */
    public function canada(){
        $cate_id = 2;
        $data = Db::name('hall')
            ->where(array('status'=>1,'cate'=>$cate_id))
            ->select();
        //计算在线人数总人数
        $online_all = Db::name('hall')
            ->sum('online');
        //计算在线人数本种游戏类型的人数
        $online_cate =Db::name('hall')
            ->where(array('status'=>1,'cate'=>$cate_id))
            ->sum('online');
        $return_data['online_all']  =  $online_all;
        $return_data['online_cate'] = $online_cate;
        $return_data['data']        = $data;
        $ads = $this->get_ads();
        return view('',[
            'return_data'=>$return_data,
            'cate'=>$cate_id,
            'ads'=>$ads
        ]);

    }

    /**
     * 加拿大28房间
     */
    public function canada_hall(Request $request){
        $cate = 2;
        $uid     = session('uid');
        $hall    = $request->param('hall');
        web_check();
        $user_info = Db::name('member')->field('money,head,mobile')->where('id',$uid)->find();
        $balance  = $user_info['money'];
        if($user_info['head']) {
            $head = Config::get('img_url') . $user_info['head'];
        }else{
            $head="";
        }
        $username = $user_info['mobile'];

        $number = getNumberCache('canada');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr_s = explode(',',$numbers['number']);
        $number_arr = getCanadaNum($number_arr_s);
        //计算下次期数 和 开间时间
        $ssc = setCanadaStageTime($stage,$numbers['dateline']);
        $total_number = array_sum($number_arr);
        $dx = eightDx($total_number); //判断大小
        $ds = eightDs($total_number);//判断单双
        $bo = eightBo($total_number)."波";//判断波色
        $detail = $total_number.",".$dx.",".$ds.","."--".",".$bo;
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$ssc['stage_next'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-10),
            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
            'detail'=>$detail,
            'balance'=>$balance,
            'head'=>$head
        ];
       // $next = $this->get_next(2);
//        $stage_data = [
//            'stage'=>$stage,
//            'stage_next'=>$next['stage'],
//            'number'=>$number_arr,
//            'number_sum'=>array_sum($number_arr),
//            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
//            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
//            'detail'=>$detail,
//            'balance'=>$balance,
//            'head'=>$head
//        ];
        //获取赔率
        $odds = Db::name('bet')
            ->field('title,type')
            ->where(array('cate'=>$cate))
            ->select();
        foreach ($odds as $k=>$v){
            $name= Db::name('odds')
                ->where(array('type'=>$v['type'],'cate'=>$cate,'hall'=>$hall))
                ->select();
            foreach ($name as $kk=>$vv){
                $name[$kk]['rate'] =number_format($vv['rate'],3);
            }
            if($name){
                $odds[$k]['name'] = $name;
            }else{
                unset($odds[$k]);
            }
        }
        $odds = array_values($odds);
        $room = Cache::get('chat_room'.$cate.$hall);
        if(!$room){
            $room = "";
        }else {
            $room = array_slice($room, -3);
        }
        $hall_info = Db::name('hall')
            ->field('name,chat_id')
            ->where(['cate'=>$cate,'hall'=>$hall])
            ->find();
        $hall_name = $hall_info['name'];
        $room_id   = $hall_info['chat_id'];
        $number = Db::name('at_canada')
            ->limit(8)
            ->order('id desc')
            ->select();
        foreach ($number as $k=>$v){
            $number_arr_s = explode(',',$v['number']);
            $total_number = array_sum(getCanadaNum($number_arr_s));
            $dx = eightDx($total_number); //判断大小
            $ds = eightDs($total_number);//判断单双
            $bo = eightBo($total_number);//判断波色
            $detail = $total_number.",".$dx.",".$ds.","."--".",".$bo;
            $number[$k]['number']   = getCanadaNum($number_arr_s);
            $number[$k]['number_sum'] = $total_number;
            $number[$k]['detail']   = $detail;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds,
            'hall'=>$hall,
            'room'=>$room,
            'hall_name'=>$hall_name,
            'cate'=>$cate,
            'room_id'=>$room_id,
            'username'=>$username,
            'number'=>$number
        ]);

    }

    /**
     * 北京赛车
     */
    public function car(){
        $cate_id = 3;
        $data = Db::name('hall')
            ->where(array('status'=>1,'cate'=>$cate_id))
            ->select();
        //计算在线人数总人数
        $online_all = Db::name('hall')
            ->sum('online');
        //计算在线人数本种游戏类型的人数
        $online_cate =Db::name('hall')
            ->where(array('status'=>1,'cate'=>$cate_id))
            ->sum('online');
        $return_data['online_all']  =  $online_all;
        $return_data['online_cate'] = $online_cate;
        $return_data['data']        = $data;
        $ads = $this->get_ads();
        return view('',[
            'return_data'=>$return_data,
            'cate'=>$cate_id,
            'ads'=>$ads
        ]);

    }

    /**
     * 北京赛车房间
     */
    public function car_hall(Request $request){
        web_check();
        $cate    =3;
        $uid     = session('uid');
        $hall    = $request->param('hall');
        $user_info = Db::name('member')->field('money,head,mobile')->where('id',$uid)->find();
        $balance = $user_info['money'];
        $head    = Config::get('img_url').$user_info['head'];
        $username = $user_info['mobile'];
        $number = getNumberCache('car');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        //计算下次期数 和 开间时间
  //      $ssc = setCarStageTime($stage,$numbers['dateline']);
        $ssc = setCarStageTime($stage,$numbers['dateline']);
        $ling = $number_arr[0]+$number_arr[1];//冠亚军和值
        $yi  = carDx($ling); //判断大小
        $er  = carDs($ling);//判断单双
        $san = longfu($number_arr[0],$number_arr[9]);//判断龙虎
        $si  = longfu($number_arr[1],$number_arr[8]);//判断龙虎
        $wu  = longfu($number_arr[2],$number_arr[7]);//判断龙虎
        $liu = longfu($number_arr[3],$number_arr[6]);//判断龙虎
        $qi  = longfu($number_arr[4],$number_arr[5]);//判断龙虎
        $detail = $ling.",".$yi.",".$er.",".$san.",". $si.",".$wu.",".$liu.",". $qi;
//        $stage_data = ['stage'=>$stage,
//         'stage_next'=>$ssc['stage_next'],
//         'number'=>$number_arr,
//         'number_sum'=>$number_arr[0] + $number_arr[1],
//         'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-10),
      //    'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//         'detail'=>$detail，
//        'balance'=>$balance,
//            'head'=>$head
//        ];
        $next = $this->get_next(3);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>$detail,
            'balance'=>$balance,
            'head'=>$head
        ];
        //获取赔率
        $odds = Db::name('bet')
            ->field('title,type')
            ->where(array('cate'=>$cate))
            ->select();
        foreach ($odds as $k=>$v){
            $name= Db::name('odds')
                ->where(array('type'=>$v['type'],'cate'=>$cate,'hall'=>$hall))
                ->select();
            foreach ($name as $kk=>$vv){
                $name[$kk]['rate'] =number_format($vv['rate'],3);
            }
            if($name){
                $odds[$k]['name'] = $name;
            }else{
                unset($odds[$k]);
            }
        }
        $odds = array_values($odds);
        $room = Cache::get('chat_room'.$cate.$hall);
        if(!$room){
            $room = "";
        }else {
            $room = array_slice($room, -3);
        }
        $hall_info = Db::name('hall')
            ->field('name,chat_id')
            ->where(['cate'=>$cate,'hall'=>$hall])
            ->find();
        $hall_name = $hall_info['name'];
        $room_id   = $hall_info['chat_id'];

        $number = Db::name('at_car')
            ->limit(5)
            ->order('id desc')
            ->select();
        foreach ($number as $k=>$v){
            $number_arr = explode(',',$v['number']);
            $ling = $number_arr[0]+$number_arr[1];//冠亚军和值
            $yi  = carDx($ling); //判断大小
            $er  = carDs($ling);//判断单双
            $san = longfu($number_arr[0],$number_arr[9]);//判断龙虎
            $si  = longfu($number_arr[1],$number_arr[8]);//判断龙虎
            $wu  = longfu($number_arr[2],$number_arr[7]);//判断龙虎
            $liu = longfu($number_arr[3],$number_arr[6]);//判断龙虎
            $qi  = longfu($number_arr[4],$number_arr[5]);//判断龙虎
            $detail = $ling.",".$yi.",".$er.",".$san.",". $si.",".$wu.",".$liu.",". $qi;
            $number[$k]['detail']  = $detail;
            $number[$k]['number']  = $number_arr;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds,
            'hall'=>$hall,
            'room'=>$room,
            'hall_name'=>$hall_name,
            'cate'=>$cate,
            'room_id'=>$room_id,
            'username'=>$username,
            'number'=>$number
        ]);
    }

    /**
     * 幸运飞艇
     */
    public function airship(){
        $cate_id = 4;
        $data = Db::name('hall')
            ->where(array('status'=>1,'cate'=>$cate_id))
            ->select();
        //计算在线人数总人数
        $online_all = Db::name('hall')
            ->sum('online');
        //计算在线人数本种游戏类型的人数
        $online_cate =Db::name('hall')
            ->where(array('status'=>1,'cate'=>$cate_id))
            ->sum('online');
        $return_data['online_all']  =  $online_all;
        $return_data['online_cate'] = $online_cate;
        $return_data['data']        = $data;
        $ads = $this->get_ads();
        return view('',[
            'return_data'=>$return_data,
            'cate'=>$cate_id,
            'ads'=>$ads
        ]);

    }

    /**
     * 幸运飞艇房间
     */
    public function airship_hall(Request $request){
        web_check();
        $cate    =4;
        $uid     = session('uid');
        $hall    = $request->param('hall');
        $user_info = Db::name('member')->field('money,head,mobile')->where('id',$uid)->find();
        $balance = $user_info['money'];
        $head    = Config::get('img_url').$user_info['head'];
        $username = $user_info['mobile'];
        $number = getNumberCache('airship');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        //计算下次期数 和 开间时间
      //  $ssc = setAirshipStageTime($stage,$numbers['dateline']);
        $ling = $number_arr[0]+$number_arr[1];//冠亚军和值
        $yi  = carDx($ling); //判断大小
        $er  = carDs($ling);//判断单双
        $san = longfu($number_arr[0],$number_arr[9]);//判断龙虎
        $si  = longfu($number_arr[1],$number_arr[8]);//判断龙虎
        $wu  = longfu($number_arr[2],$number_arr[7]);//判断龙虎
        $liu = longfu($number_arr[3],$number_arr[6]);//判断龙虎
        $qi  = longfu($number_arr[4],$number_arr[5]);//判断龙虎
        $detail = $ling.",".$yi.",".$er.",".$san.",". $si.",".$wu.",".$liu.",". $qi;
//        $stage_data = ['stage'=>$stage,
//         'stage_next'=>$ssc['stage_next'],
//         'number'=>$number_arr,
//         'number_sum'=>$number_arr[0] + $number_arr[1],
//         'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-10),
        //    'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//         'detail'=>$detail，
//        'balance'=>$balance,
//            'head'=>$head
//        ];
        $next = $this->get_next(4);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>$detail,
            'balance'=>$balance,
            'head'=>$head
        ];
        //获取赔率
        $odds = Db::name('bet')
            ->field('title,type')
            ->where(array('cate'=>$cate))
            ->select();
        foreach ($odds as $k=>$v){
            $name= Db::name('odds')
                ->where(array('type'=>$v['type'],'cate'=>$cate,'hall'=>$hall))
                ->select();
            foreach ($name as $kk=>$vv){
                $name[$kk]['rate'] =number_format($vv['rate'],3);
            }
            if($name){
                $odds[$k]['name'] = $name;
            }else{
                unset($odds[$k]);
            }
        }
        $odds = array_values($odds);
        $room = Cache::get('chat_room'.$cate.$hall);
        if(!$room){
            $room = "";
        }else {
            $room = array_slice($room, -3);
        }
        $hall_info = Db::name('hall')
            ->field('name,chat_id')
            ->where(['cate'=>$cate,'hall'=>$hall])
            ->find();
        $hall_name = $hall_info['name'];
        $room_id   = $hall_info['chat_id'];
        $number = Db::name('at_airship')
            ->limit(5)
            ->order('id desc')
            ->select();
        foreach ($number as $k=>$v){
            $number_arr = explode(',',$v['number']);
            $ling = $number_arr[0]+$number_arr[1];//冠亚军和值
            $yi  = carDx($ling); //判断大小
            $er  = carDs($ling);//判断单双
            $san = longfu($number_arr[0],$number_arr[9]);//判断龙虎
            $si  = longfu($number_arr[1],$number_arr[8]);//判断龙虎
            $wu  = longfu($number_arr[2],$number_arr[7]);//判断龙虎
            $liu = longfu($number_arr[3],$number_arr[6]);//判断龙虎
            $qi  = longfu($number_arr[4],$number_arr[5]);//判断龙虎
            $detail = $ling.",".$yi.",".$er.",".$san.",". $si.",".$wu.",".$liu.",". $qi;
            $number[$k]['detail']  = $detail;
            $number[$k]['number']  = $number_arr;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds,
            'hall'=>$hall,
            'room'=>$room,
            'hall_name'=>$hall_name,
            'cate'=>$cate,
            'room_id'=>$room_id,
            'username'=>$username,
            'number'=>$number
        ]);
    }

    /**
     * 重庆时时彩
     */
    public function ssc(){
        $uid     = session('uid');
        $cate = 5;
        if(!$uid){
            $balance = '0.00';
        }else{
            $balance  = Db::name('member')->where('id',$uid)->value('money');
        }
        $number = getNumberCache('ssc');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        //计算下次期数 和 开间时间
       // $ssc = setSscStageTime($stage);
        $he = array_sum($number_arr);
        $dx = sscDx($he); //判断大小
        $ds = sscDs($he);//判断单双
        $longfu = sscLh($number_arr);
        $first_three    = take_three($number_arr,0);
        $r_first_three  = judge_three($first_three);
        $second_three   = take_three($number_arr,1);
        $r_second_three = judge_three($second_three);
        $third_three = take_three($number_arr,2);
        $r_third_three = judge_three($third_three);
        $niuniu = niuNiu($number_arr);
        $detail = $he.",".$dx.",".$ds.",".$longfu.",". $r_first_three.",".$r_second_three.",".$r_third_three.",".$niuniu;
//        $stage_data = [
//            'stage'=>$stage,
//            'stage_next'=>$ssc['stage_next'],
//            'number'=>$number_arr,
//            'number_sum'=>$number_arr[0] + $number_arr[1],
//            'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-5),
//            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//            'detail'=>explode(',',$detail)
//        ];
        $next = $this->get_next(5);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>explode(',',$detail)
        ];

        //获取盘
        $pan = Db::name('pan')
            ->field('name,pan')
            ->where('cate',$cate)
            ->select();
        $odds_pan = $pan;
        $titles = Db::name('bet')->where('cate',$cate)->column('title');
        foreach ($odds_pan as $k=>$v){
           $odds = Db::name('odds')
                ->field('type')
                ->where(array('pan'=>$v['pan'],'cate'=>$cate))
                ->group('type')
                ->select();
            foreach ($odds as $kk=>$vv){
                $odds[$kk]['type_name'] = $titles[$vv['type']-1];
                $odds[$kk]['type_odd']  =Db::name('odds')
                                    ->where(array('pan'=>$v['pan'],'cate'=>$cate,'type'=>$vv['type']))
                                    ->select();
            }
            $odds_pan[$k]['titles'] =$odds;
        }

        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds_pan,
            'pan' =>$pan,
            'balance'=>$balance,
            'cate'=>$cate
        ]);

    }

    /**
     * 天津时时彩
     */
    public function tjssc(){
        $uid     = session('uid');
        $cate = 6;
        if(!$uid){
            $balance = '0.00';
        }else{
            $balance  = Db::name('member')->where('id',$uid)->value('money');
        }
        $number = getNumberCache('tjssc');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        //计算下次期数 和 开间时间
 //       $ssc = settjsscStageTime($stage,$numbers['dateline']);
        $he = array_sum($number_arr);
        $dx = sscDx($he); //判断大小
        $ds = sscDs($he);//判断单双
        $longfu = sscLh($number_arr);
        $first_three = take_three($number_arr,0);
        $r_first_three = judge_three($first_three);
        $second_three = take_three($number_arr,1);
        $r_second_three = judge_three($second_three);
        $third_three = take_three($number_arr,2);
        $r_third_three = judge_three($third_three);
        $niuniu = niuNiu($number_arr);
        $detail = $he.",".$dx.",".$ds.",".$longfu.",". $r_first_three.",".$r_second_three.",".$r_third_three.",".$niuniu;
//        $stage_data = [
//            'stage'=>$stage,
//            'stage_next'=>$ssc['stage_next'],
//            'number'=>$number_arr,
//            'number_sum'=>$number_arr[0] + $number_arr[1],
//            'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-5),
//            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//            'detail'=>explode(',',$detail)
//        ];
        $next = $this->get_next(6);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>explode(',',$detail)
        ];
        //获取盘
        $pan = Db::name('pan')
            ->field('name,pan')
            ->where('cate',$cate)
            ->select();
        $odds_pan = $pan;
        $titles = Db::name('bet')->where('cate',$cate)->column('title');
        foreach ($odds_pan as $k=>$v){
            $odds = Db::name('odds')
                ->field('type')
                ->where(array('pan'=>$v['pan'],'cate'=>$cate))
                ->group('type')
                ->select();
            foreach ($odds as $kk=>$vv){
                $odds[$kk]['type_name'] = $titles[$vv['type']-1];
                $odds[$kk]['type_odd']  =Db::name('odds')
                    ->where(array('pan'=>$v['pan'],'cate'=>$cate,'type'=>$vv['type']))
                    ->select();
            }
            $odds_pan[$k]['titles'] =$odds;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds_pan,
            'pan' =>$pan,
            'balance'=>$balance,
            'cate'=>$cate
        ]);
    }

    /**
     * 广东快乐十分
     */
    public function gd10(){
        $uid     = session('uid');
        $cate = 7;
        if(!$uid){
            $balance = '0.00';
        }else{
            $balance  = Db::name('member')->where('id',$uid)->value('money');
        }
        $number = getNumberCache('gd10');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        //计算下次期数 和 开间时间
        //$ssc = setgd10StageTime($stage,$numbers['dateline']);
        $ling = array_sum($number_arr);//冠亚军和值
        $yi  = Zgd10dx($ling); //判断大小
        $er  = Zgd10ds($ling);//判断单双
        $san = Zgd10weidx($ling);//判断龙虎
        $si  = Zgd10weids($ling);//判断龙虎
        $detail = $ling.",".$yi.",".$er.",".$san.",". $si;
//        $stage_data = [
//            'stage'=>$stage,
//            'stage_next'=>$ssc['stage_next'],
//            'number'=>$number_arr,
//            'number_sum'=>$number_arr[0] + $number_arr[1],
//            'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-5),
//            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//            'detail'=>explode(',',$detail)
//        ];
        $next = $this->get_next(7);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>explode(',',$detail)
        ];

        //获取盘
        $pan = Db::name('pan')
            ->field('name,pan')
            ->where('cate',$cate)
            ->select();
        $odds_pan = $pan;
        $titles = Db::name('bet')->where('cate',$cate)->column('title');
        foreach ($odds_pan as $k=>$v){
            $odds = Db::name('odds')
                ->field('type')
                ->where(array('pan'=>$v['pan'],'cate'=>$cate))
                ->group('type')
                ->select();
            foreach ($odds as $kk=>$vv){
                $odds[$kk]['type_name'] = $titles[$vv['type']-1];
                $odds[$kk]['type_odd']  =Db::name('odds')
                    ->where(array('pan'=>$v['pan'],'cate'=>$cate,'type'=>$vv['type']))
                    ->select();
            }
            $odds_pan[$k]['titles'] =$odds;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds_pan,
            'pan' =>$pan,
            'balance'=>$balance,
            'cate'=>$cate
        ]);
    }

    /**
     * 重庆快乐十分
     */
    public function cq10(){
        $uid     = session('uid');
        $cate = 8;
        if(!$uid){
            $balance = '0.00';
        }else{
            $balance  = Db::name('member')->where('id',$uid)->value('money');
        }
        $number = getNumberCache('cq10');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        //计算下次期数 和 开间时间
    //    $ssc = setcq10StageTime($stage,$numbers['dateline']);
        $ling = array_sum($number_arr);//冠亚军和值
        $yi  = Zgd10dx($ling); //判断大小
        $er  = Zgd10ds($ling);//判断单双
        $san = Zgd10weidx($ling);//判断龙虎
        $si  = Zgd10weids($ling);//判断龙虎
        $detail = $ling.",".$yi.",".$er.",".$san.",". $si;
//        $stage_data = ['stage'=>$stage,
//            'stage_next'=>$ssc['stage_next'],
//            'number'=>$number_arr,
//            'number_sum'=>$number_arr[0] + $number_arr[1],
//        'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-5),
//            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//            'detail'=>explode(',',$detail)
//        ];
        $next = $this->get_next(8);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>explode(',',$detail)
        ];

        //获取盘
        $pan = Db::name('pan')
            ->field('name,pan')
            ->where('cate',$cate)
            ->select();
        $odds_pan = $pan;
        $titles = Db::name('bet')->where('cate',$cate)->column('title');
        foreach ($odds_pan as $k=>$v){
            $odds = Db::name('odds')
                ->field('type')
                ->where(array('pan'=>$v['pan'],'cate'=>$cate))
                ->group('type')
                ->select();
            foreach ($odds as $kk=>$vv){
                $odds[$kk]['type_name'] = $titles[$vv['type']-1];
                $odds[$kk]['type_odd']  =Db::name('odds')
                    ->where(array('pan'=>$v['pan'],'cate'=>$cate,'type'=>$vv['type']))
                    ->select();
            }
            $odds_pan[$k]['titles'] =$odds;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds_pan,
            'pan' =>$pan,
            'balance'=>$balance,
            'cate'=>$cate
        ]);
        return view();
    }

    /**
     * 江苏快三
     */
    public function fast(){
        $uid     = session('uid');
        $cate = 9;
        if(!$uid){
            $balance = '0.00';
        }else{
            $balance  = Db::name('member')->where('id',$uid)->value('money');
        }
        $number = getNumberCache('fast');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        $he = array_sum($number_arr); //和
        $dx = FastDx($he);//判断大小
        $detail = $he.",".$dx;
 //       $ssc = setFastStageTime($stage);
//        $stage_data = [
//            'stage'=>$stage,
//            'stage_next'=>$ssc['stage_next'],
//            'number'=>$number_arr,
//            'number_sum'=>$number_arr[0] + $number_arr[1],
//            'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-5),
//            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//            'detail'=>explode(',',$detail)
//        ];
        $next = $this->get_next(9);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>explode(',',$detail)
        ];

        //获取盘
        $pan = Db::name('pan')
            ->field('name,pan')
            ->where('cate',$cate)
            ->select();
        $odds_pan = $pan;
        $titles = Db::name('bet')->where('cate',$cate)->column('title');
        foreach ($odds_pan as $k=>$v){
            $odds = Db::name('odds')
                ->field('type')
                ->where(array('pan'=>$v['pan'],'cate'=>$cate))
                ->group('type')
                ->select();
            foreach ($odds as $kk=>$vv){
                $odds[$kk]['type_name'] = $titles[$vv['type']-1];
                $odds[$kk]['type_odd']  =Db::name('odds')
                    ->where(array('pan'=>$v['pan'],'cate'=>$cate,'type'=>$vv['type']))
                    ->select();
            }
            $odds_pan[$k]['titles'] =$odds;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds_pan,
            'pan' =>$pan,
            'balance'=>$balance,
            'cate'=>$cate
        ]);
        return view();
    }

    /**
     * 广东11选5
     */
    public function gd11(){
        $uid     = session('uid');
        $cate    = 10;
        if(!$uid){
            $balance = '0.00';
        }else{
            $balance  = Db::name('member')->where('id',$uid)->value('money');
        }
        $number = getNumberCache('gd11');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        //计算下次期数 和 开间时间
      //  $ssc = setgd11StageTime($stage,$numbers['dateline']);
        $ling = array_sum($number_arr);//冠亚军和值
        $yi  = gd11zdx($ling); //判断大小
        $er  = gd11ds($ling);//判断单双
        $san = gd11lf($number_arr);//判断龙虎
        $detail = $ling.",".$yi.",".$er.",".$san;
//        $stage_data = [
//            'stage'=>$stage,
//            'stage_next'=>$ssc['stage_next'],
//            'number'=>$number_arr,
//            'number_sum'=>$number_arr[0] + $number_arr[1],
//            'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-5),
//            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
//            'detail'=>explode(',',$detail)
//        ];
        $next = $this->get_next(10);
        $stage_data = [
            'stage'=>$stage,
            'stage_next'=>$next['stage'],
            'number'=>$number_arr,
            'number_sum'=>array_sum($number_arr),
            'dateline'=>date('Y-m-d H:i:s',strtotime($next['dateline'])-10),
            'lottery_time'=>date('Y-m-d H:i:s',strtotime($next['dateline'])+20),
            'detail'=>explode(',',$detail)
        ];
        //获取盘
        $pan = Db::name('pan')
            ->field('name,pan')
            ->where('cate',$cate)
            ->select();
        $odds_pan = $pan;
        $titles = Db::name('bet')->where('cate',$cate)->column('title');
        foreach ($odds_pan as $k=>$v){
            $odds = Db::name('odds')
                ->field('type')
                ->where(array('pan'=>$v['pan'],'cate'=>$cate))
                ->group('type')
                ->select();
            foreach ($odds as $kk=>$vv){
                $odds[$kk]['type_name'] = $titles[$vv['type']-1];
                $odds[$kk]['type_odd']  =Db::name('odds')
                    ->where(array('pan'=>$v['pan'],'cate'=>$cate,'type'=>$vv['type']))
                    ->select();
            }
            $odds_pan[$k]['titles'] =$odds;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds_pan,
            'pan' =>$pan,
            'balance'=>$balance,
            'cate'=>$cate
        ]);
    }

    /**
     * 香港六合彩
     */
    public function hk(){
        $uid     = session('uid');
        $cate    = 11;
        if(!$uid){
            $balance = '0.00';
        }else{
            $balance  = Db::name('member')->where('id',$uid)->value('money');
        }
        $number = getNumberCache('hk');
        $stage= key($number); //获取最新一期的key（即期数）
        $numbers =$number[$stage]; //获取最新开奖号
        $number_arr = explode(',',$numbers['number']);
        //计算下次期数 和 开间时间
        $ssc = setHkStageTime($stage,$numbers['dateline']);
        $hk_time = Db::name('cate_hk')->where('id',1111)->find();

        //计算下次期数 和 开间时间
        $detail = get_zodiac($number_arr);
        $stage_data = ['stage'=>$stage,
            'stage_next'=>$ssc['stage_next'],
            'number'=>$number_arr,
            'number_sum'=>$number_arr[0] + $number_arr[1],
//            'dateline'=>date('Y-m-d H:i:s',$ssc['dateline']-10),
//            'lottery_time'=>date('Y-m-d H:i:s',$ssc['dateline']+20),
             'dateline'=>$hk_time['open'],
            'lottery_time'=>$hk_time['dateline'],
            'detail'=>explode(',',$detail)
        ];


        //获取盘
        $pan = Db::name('pan')
            ->field('name,pan')
            ->where('cate',$cate)
            ->select();
        $odds_pan = $pan;
        $titles = Db::name('bet')->field('type,title')->where('cate',$cate)->select();
        $arr = [];
        foreach ($titles as $kk=>$vv){
            $arr[$vv['type']] = $vv['title'];
        }
        foreach ($odds_pan as $k=>$v){
            $odds = Db::name('odds')
                ->field('type')
                ->where(array('pan'=>$v['pan'],'cate'=>$cate))
                ->group('type')
                ->select();
            foreach ($odds as $kk=>$vv){
                $odds[$kk]['type_name'] = $arr[$vv['type']];
                $odds[$kk]['type_odd']  =Db::name('odds')
                    ->where(array('pan'=>$v['pan'],'cate'=>$cate,'type'=>$vv['type']))
                    ->select();
            }
            $odds_pan[$k]['titles'] =$odds;
        }
        return view('',[
            'stage_data'=>$stage_data,
            'odds'=>$odds_pan,
            'pan' =>$pan,
            'balance'=>$balance,
            'cate'=>$cate
        ]);
        return view();
    }

    /**
     * 大厅
     */
    public function hall_one(){
        return view();
    }
    

    /**
     * 投注
     */
    public function betting(Request $request){
        $cate   = $request->post('cate');
        $stage  = $request->post('stage');
        $hall   = $request->post('hall');
        $list   = $request->post('list');
        $ip     = get_client_ip();
        $uid    = session('uid');
        if(!$uid || !$stage  || !$hall ||!$list ||!$cate ){
            json_return(204,'缺少参数');
        }
        //获取会员信息
        $user = Members::get($uid);
        if($user['m_status']==1){
            json_return(201,'您的账号已被冻结!');
        }
        switch ($cate){
            case 1:
                $table_name = "at_eight";
                break;
            case 2:
                $table_name = "at_canada";
                break;
            case 3:
                $table_name = "at_car";
                break;
            case 4:
                $table_name = "at_airship";
                break;
            case 5:
                $table_name = "at_ssc";
                break;
            case 6:
                $table_name = "at_tjssc";
                break;
            case 7:
                $table_name = "at_gd10";
                break;
            case 8:
                $table_name = "at_cq10";
                break;
            case 9:
                $table_name = "at_fast";
                break;
            case 10:
                $table_name = "at_gd11";
                break;
            case 11:
                $table_name = "at_hk";
                break;
        }
        $is_stage = Db::name($table_name)->order('id desc')->find()['stage'];
        if($stage<=$is_stage){
            json_return(201,'已经封盘');
        }

        $list_d = json_decode($list, true);
        try {
            $list_d = json_decode($list, true);
        }catch (\Exception $e){
            json_return(201,'list格式不对');
        }
        if(!is_array($list_d)){

            json_return(201,'list格式不对');
        }
//        if(count($list_d)>30){
//            json_return(201,'一次投注不能超过30注');
//        }
        foreach ($list_d as $k=>$v){
            $map['cate'] = $cate;
            $map['type'] = $v['type'];
            $map['hall'] = $hall;
            $bets = Db::name('bet_admin')
                ->field('from,to')
                ->where($map)
                ->find();
            $map_s['hall'] = $hall;
            $map_s['stage']  = $stage;
            $map_s['number'] = $v['number'];
            $map_s['uid']    = $uid;
            $map_s['type']   = $v['type'];
            $now_money = Db::name('single')->where($map_s)->sum('money');
            if($now_money>=$bets['to'] ){
                json_return(201,'金额超出限制');exit();
            }
            if($v['money']<$bets['from']){
                json_return(201,'最低投注金额为'.$bets['from']);exit();
            }
            if($v['money']>$bets['to']){
                json_return(201,'最高投注金额为'.$bets['to']);exit();
            }
        }
        $time = time();
        foreach ($list_d as $k=>$v){
            $money[] = $v['money'];
            $data[]=[
                'uid'   =>$uid,
                'stage' =>$stage,
                'cate'  =>$cate,
                'type'  =>$v['type'],
                'hall'  =>$hall,
                'number'=>$v['number'],
                'money' =>$v['money'],
                'ip'    =>$ip,
                'create_at'=>$time,
                'login_way'=>3
            ];
        }
        $total_money = array_sum($money);
        if($total_money>$user->money){
            json_return(207,'余额不足');
        }
        $chat_content = [];
        if($cate<=4) {
            $chat_room = Cache::get('chat_room'.$cate.$hall);
            if(!$chat_room) {
                foreach ($list_d as $k=>$v) {
                    $room['head']   =  $user['head'];
                    $room['number'] = $v['number'];
                    $room['stage'] = $stage;
                    $room['money'] = $v['money'];
                    $room['play']  = $v['play'];
                    $room['time']  = date('m-d H:i:s');
                    $room['title'] = $v['play'];;
                    $room['uid']   = $uid;
                    $room['nickname'] = $user['nickname'];
                    $chat_room[]    = $room;
                    $chat_content[] = $room;
                    Cache::store('redis')->set('chat_room' . $cate . $hall, $chat_room);
                }
            }else{
                foreach ($chat_room as $k=>$v){
                    if($v['stage']!=$stage){
                        unset($chat_room[$k]);
                    }
                }
                foreach ($list_d as $k=>$v) {
                    $room['head']   =  $user['head'];
                    $room['number'] = $v['number'];
                    $room['stage'] = $stage;
                    $room['money'] = $v['money'];
                    $room['play']  = $v['play'];
                    $room['time']  = date('m-d H:i:s');
                    $room['title'] = $v['play'];;
                    $room['uid']   = $uid;
                    $room['nickname'] = $user['nickname'];
                    $chat_room[] = $room;
                    $chat_content[] = $room;
                    Cache::store('redis')->set('chat_room' . $cate . $hall, $chat_room);
                }
            }
            $chat_content = json_encode($chat_content);
            $content = ['info'=> $chat_content];
            $ext = $content;
            $map0['cate'] = $cate;
            $map0['hall'] = $hall;
            $chat_id = Db::name('hall')->where($map0)->value('chat_id');
            $username = [$chat_id];
            $HX = new HX();
            $result = $HX->yy_hxSend("admin", $username, $content, "chatrooms", $ext);
        }
        Db::startTrans();
        try{
            Db('single')->insertAll($data);
            $moneys =  $user->money-($total_money);
            Db('member')->where('id',$uid)->update(['money'=>$moneys]);
            addDetail($uid,2,$total_money,$moneys,5,$cate,'期数'.$stage,$hall);
            Db::commit();
            $data['money'] = $moneys;
            json_return(200,'投注成功',$data);
        } catch (\Exception $e) {
            Db::rollback();
            json_return(500,'投注失败');
        }


    }

    /**
     * 聊天室
     */
    public function get_chat(Request $request){
        $cate   = $request->post('cate');
        $hall   = $request->post('hall');
        $data   = Cache::get('chat_room'.$cate.$hall);
        if($data){
            $daa = array_slice($data,-1);
            if($daa){
                json_return(200,'成功',$daa);
            }else{
                json_return(201,'失败');
            }
        }else{
            json_return(201,'失败');
        }
    }



}